<template>
  <div class="edit-container">
    <el-card class="edit-card">
      <div slot="header" class="clearfix">
        <span>编辑</span>
      </div>
      <div class="edit-main">
        <el-form
          :model="form"
          ref="ruleForm"
          :rules="rules"
          label-width="120px"
        >
          <el-form-item label="类型名称" prop="name">
            <el-input
              v-model="form.name"
              placeholder="请输入类型名称"
            ></el-input>
          </el-form-item>

          <el-form-item label="备注" prop="remark">
            <el-input
              typepe="textarea"
              v-model="form.remark"
              placeholder="请输入备注"
            ></el-input>
          </el-form-item>
          <!-- 状态 -->
          <el-form-item label="状态" prop="status">
            <el-select v-model="form.status" placeholder="请选择状态">
              <el-option
                v-for="item in statusList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >提交</el-button
            >
            <el-button @click="resetForm('ruleForm')">重置</el-button>
            <el-button type="warning" @click="goBack()">返回</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import { opusCategory } from "@/api/opusCategory";
export default {
  data() {
    return {
      form: {
        id: "",
        name: "",
        remark: "",
        status: "",
      },
      rules: {
        name: [{ required: true, message: "请输入类型名称", trigger: "blur" }],
        status: [{ required: true, message: "请选择状态", trigger: "change" }],
        remark: [{ required: false }], // 备注可以不填
      },
      size: "medium",
      statusList: [
        { name: "启用", id: 1 },
        { name: "禁用", id: 0 },
      ],
    };
  },
  created() {
    this.getOpusCategory();
  },
  methods: {
    getOpusCategory() {
      // 获取当前路由参数
      const routeParams = this.$route.params;
      //   console.log(routeParams.id);

      // 判断是否包含id
      if (routeParams.id) {
        // 获取当前id
        const id = routeParams.id;
        // console.log(id);
        // 根据id获取当前数据
        opusCategory.find(id).then((res) => {
          this.form = res.data;
        });
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // console.log("submit!");
          // 提交逻辑，例如发送到后端
          opusCategory
            .update(this.form)
            .then((res) => {
              this.$message({
                message: res.msg,
                type: "success",
              });
              this.$router.push({
                name: "Copyright",
              });
            })
            .catch(() => {
              this.$message({
                message: res.msg,
                type: "danger",
              });
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    goBack() {
      this.$router.go(-1);
    },
  },
};
</script>

<style scoped>
.clearfix {
  text-align: center;
}

.input-style {
  width: 100%; /* 输入框宽度一致 */
}

/* 使每个表单项的标签及内容居中 */
.el-form-item__content {
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
